/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogModificarBono.java
 *
 * Created on 16/04/2011, 05:48:55 PM
 */

package frontera.interfaz.dialogo;

import entidad.Bono;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Sebastian
 */
public class DialogModificarBono extends javax.swing.JDialog {

    /** Creates new form DialogModificarBono */
    public DialogModificarBono(java.awt.Frame parent, boolean modal,JPanel panel,Bono bono,
                               List<Bono> bonosTemporales) {
        super(parent, modal);
        inicializarBoxDias();
        initComponents();
        setLocationRelativeTo(null);
        iniciarVariables(panel,bono,bonosTemporales);
        setFields();
    }

    private void iniciarVariables(JPanel panel,Bono bono, List<Bono> bonosTemporales){

        this.setTitle("Creando Bono");

        DialogModificarBono.panel = panel;
        DialogModificarBono.bonosTemporales = bonosTemporales;

        DialogModificarBono.bono = bono;

        Calendar cal = Calendar.getInstance();
        if(cal.get(Calendar.DATE)>15){
            boxDiaAdicion.setSelectedIndex(cal.get(Calendar.DATE)-16);
        }
        else{
            boxDiaAdicion.setSelectedIndex(cal.get(Calendar.DATE)-1);
        }
        
    }

    private void inicializarBoxDias(){
        
        Calendar cal = Calendar.getInstance();

        int maximoDia = cal.getActualMaximum(Calendar.DATE);

        if(cal.get(Calendar.DATE)>15){
            arregloBox = new String[maximoDia-15];
            for(int i=0;i<maximoDia-15;i++){
                arregloBox[i]=Integer.toString(i+16);
            }
        }
        else{
            arregloBox = new String[15];
            for(int i=0;i<15;i++){                
                arregloBox[i]=Integer.toString(i+1);
            }
        }

    }

    private void setFields(){
        fieldDescripcion.setText(bono.getDescripcion());
        fieldValor.setText(Float.toString(bono.getCantidadBono()));
        Calendar cal = Calendar.getInstance();
        if(cal.get(Calendar.DATE)>15){
            boxDiaAdicion.setSelectedIndex(cal.get(Calendar.DATE)-16);
        }
        else{
            boxDiaAdicion.setSelectedIndex(cal.get(Calendar.DATE)-1);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldValor = new javax.swing.JTextField();
        botonCancelar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        fieldDescripcion = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        boxDiaAdicion = new JComboBox(arregloBox);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        jLabel3.setText("Valor");

        jLabel4.setText("Descripcion");

        jLabel2.setText("Dia de adicion del Bono");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/updBonos.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(boxDiaAdicion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(96, 96, 96)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(fieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(fieldDescripcion)))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCancelar)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(boxDiaAdicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(fieldDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(fieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(32, 32, 32)))
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelar)
                    .addComponent(botonGuardar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.setVisible(false);
        this.dispose();
}//GEN-LAST:event_botonCancelarActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed

        bono.setDescripcion(fieldDescripcion.getText());
        bono.setDiaDelMes(Integer.parseInt((String)boxDiaAdicion.getSelectedItem()));
        try{
            float valor = Float.parseFloat(fieldValor.getText());
            if(valor>=0){
                bono.setCantidadBono(valor);
            }
            else{
                throw new NumberFormatException();
            }
        } catch(Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Debe ingresar un Valor que represente una cantidad de dinero",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_botonGuardarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogModificarBono dialog = new DialogModificarBono(new javax.swing.JFrame(), true,panel,bono,bonosTemporales);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JComboBox boxDiaAdicion;
    private javax.swing.JTextField fieldDescripcion;
    private javax.swing.JTextField fieldValor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
    private String[] arregloBox;
    static private JPanel panel;
    static private Bono bono;
    static private List<Bono> bonosTemporales;

}
